package services

import (
	"context"
	"wxapi/internal/svc"
	"wxapi/models"

	"fmt"

	"gitee.com/dromara/carbon/v2"
)

func getOrderSnKey() string {
	return fmt.Sprintf("Order:SN:%s", carbon.Now().Format("Ymd"))
}

func GetOrderItemsByItems(ctx context.Context, svc *svc.ServiceContext, itmeId int64, date carbon.Carbon) ([]*models.OrderItems, error) {
	items := make([]*models.OrderItems, 0)

	startTime := date.SetTime(0, 0, 0)
	endTime := date.SetTime(23, 59, 59)

	svc.Mysql.Where("begin_time BETWEEN ? AND ? AND skill_id = ?", startTime.ToDateTimeString(), endTime.ToDateTimeString(), itmeId).Find(&items)
	return items, nil
}

func CreateOrder(ctx context.Context, svc *svc.ServiceContext, order *models.Orders) error {
	err := svc.Mysql.Create(order).Error
	if err != nil {
		return err
	}
	return nil
}

func CreateOrderSn(ctx context.Context, svc *svc.ServiceContext) string {
	c, _ := svc.Redis.Incr(ctx, getOrderSnKey()).Result()
	return fmt.Sprintf("%s%05d", carbon.Now().Format("YmdHis"), c)
}
